/**
 * 分数来源选择
 */

var selector;

//页面载入初始化
$( "body" ).ready( function( e )
{
	selector = new SourceSelector();
	selector.cur = selector;
	selector.init();
} );

//窗口大小改变
$( window ).resize( function( e )
{
	if( selector.visibled() )
	{
		selector.relocate();
	}
} );

function selector_open( pl, group )
{
	selector.open( pl, group );
}

function SourceSelector()
{
	this.cur = null;
	this.html = "<div rel='POPUP_WINDOW_MASK' id='__DIV_SELECTOR_MASK' style='display:block;position:absolute;z-index:8999;background-color:#000;filter:alpha(opacity=20);opacity:0.2;'></div>" +
			"<div rel='POPUP_WINDOW' id='__DIV_SELECTOR' " +
			"style='position:absolute;z-index:9000;'>" +
			"<dt><span>分数来源选择（选项组）</span><a id='btn_cancel'>[ 取消 ]</a> <a id='btn_enter'>[ 确定 ]</a></dt>" +
			"<ul></ul></div>";

	// 变量
	var $box;
	var $mask;
	var $list;
	var callback;

	// 初始化
	this.init = function()
	{
		$( "body" ).append( this.html );
		$box = $( "#__DIV_SELECTOR" );
		$mask = $( "#__DIV_SELECTOR_MASK" );
		$list = $box.find( ">ul:first" );

		$box.find( "#btn_cancel" ).click( { cur: this.cur }, function( e )
		{
			var pcur = e.data.cur;
			pcur.close();
		} );
		$box.find( "#btn_enter" ).click( { cur: this.cur }, function( e )
		{
			var pcur = e.data.cur;
			var a = new Array();
			$list.find( "li[class=selected]" ).each( function()
			{
				a.push( $( this ).attr( "rel" ) );
			} );

			if( typeof ( callback ) == "function" )
				callback( a );

			pcur.close();
		} );

		$box.hide();
		$mask.hide();
	};

	// 是否显示
	this.visibled = function()
	{
		return $box.visible();
	};

	// 显示单条信息
	this.open = function( selected, tar )
	{
		// 来源列表
		tar.find( "div.item_group" ).each( function( idx )
		{
			var id = $( this ).attr( "id" );
			var topic = $( this ).find( "input[name='topic']" ).val();
			var h = "<li rel=\"" + id + "\" style=\"list-style:decimal inside;\">";
			h += "<label>选项组：</label>" + topic;
			h += "</li>";
			$list.append( h );
		} );

		$list.find( "li[rel]" ).css( "cursor", "pointer" );
		$list.find( "li[rel]" ).unbind( "click" );
		$list.find( "li[rel]" ).bind( "click", function( e )
		{
			$( this ).toggleClass( "selected" );
		} );

		// 设置已经选定的项目
		var a = empty( selected ) ? [] : selected;

		$list.find( "li[rel]" ).each( function( i )
		{
			var id = $( this ).attr( "rel" );
			if( a.indexOf( id ) > -1 )
			{
				$( this ).addClass( "selected" );
			}
		} );

		this.relocate();
		$box.slideDown( 150 );
		$mask.fadeIn( 150 );
	};

	this.relocate = function()
	{
		var bw = $( "body" ).width();
		var bh = $( "body" ).height();
		var w = $box.width();
		var h = $box.height();

		$mask.width( bw );
		$mask.height( bh );
		$mask.move( 0, 0 );
		$box.move( ( bw - w ) / 2, ( bh - h ) / 2 * 0.8 );
	};

	this.close = function()
	{
		$list.empty();
		$box.slideUp( 150 );
		$mask.fadeOut( 150 );
	};

	this.setCallback = function( fn )
	{
		callback = fn;
	};
}
